--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local svrName = 'zfb_service'

local zfb_robotMgr = class("zfb_robotMgr")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function zfb_robotMgr:ctor(message, robotMgr)
	print("---------------------zfb_robotMgr1112")
	self.robotMgr = robotMgr	
	self.msg = message	
	self.tbService = {}
	self.tableRobots = {}
	self.tbEnableCheckSeat = {}
	self.tbCount = {}
	self.gameid = 9
	self:register()

	self.createNum = 20
	self:reqAddRoot(self.createNum)
	self:checkAddRoot()		
end

--注册本服务里的消息
function zfb_robotMgr:register()
	self.msg:registerSelf('add_robot',handler(self,self.addRobot))
	self.msg:registerSelf('del_robot',handler(self,self.delRobot))

	self.msg:registerSelf('player_enter_table_res',handler(self,self.player_enter_table_res))
	self.msg:registerSelf('player_leave_table_res',handler(self,self.player_leave_table_res))
	self.msg:registerSelf('nt_start',handler(self,self.nt_start))
	self.msg:registerSelf('nt_settle',handler(self,self.nt_settle))
	--
end

function zfb_robotMgr:addRobot(data,user)
	print("____addRobot_____zhongfabai__",data)
	local playerid = data.mid
	if self.tbService[playerid] then 
		self:delRobot(data)
	end

	local sql = string.format("select game from userinfo where uid=%d;", playerid);
	local res = skynet.call("mysql_service", "lua", "execute", sql)
	data.game = 0
	data.gameid = self.gameid
	if res and res[1] and res[1].game then
		data.game = res[1].game 
	end

	local service = skynet.newservice(svrName)
	skynet.call(service, 'lua', 'start', data, user)	
	self.tbService[playerid] = service
	print()
end

function zfb_robotMgr:delRobot(data)
	local playerid = data.mid
	local service = self.tbService[playerid]
	if service then
		skynet.send(service, 'lua', 'exit')
		self.tbService[playerid] = nil
	end
end

function zfb_robotMgr:player_enter_table_res(data)
	self.tableRobots[data.tableid] = self.tableRobots[data.tableid] or {}
	table.insert(self.tableRobots[data.tableid], data.playerid)
end

function zfb_robotMgr:player_leave_table_res(data)
	if self.tableRobots[data.tableid] then
		for k, v in ipairs(self.tableRobots[data.tableid] ) do
			if v == data.playerid then
				table.remove(self.tableRobots[data.tableid], k)
				print("------------------------------->player_leave_table_res2")
				self:delRobot({mid = v})
				break
			end			
		end
	end
end

function zfb_robotMgr:nt_start(data)
	self.tbEnableCheckSeat[data.tableid] = true
end

function zfb_robotMgr:nt_settle(data)
	if self.tbEnableCheckSeat[data.table_id] == true or self.tbEnableCheckSeat[data.table_id] == nil then
		self.tbEnableCheckSeat[data.table_id] = false
		data.seat_settle = data.seat_settle or {}
		local seatData = {}
		seatData.tableid = data.table_id
		seatData.seat_settle = data.seat_settle 
		self:checkSeat(seatData)
	end
	
end 

--检查要坐下还是站起
function zfb_robotMgr:checkSeat(data)
	local seatLimitNum = 5
	local seatNum = table.len(data.seat_settle)
	--print("--------------checkSeat", seatNum)
	if seatNum < seatLimitNum then--坐下
		for i=1, seatLimitNum - table.len(data.seat_settle)  do
			local playerid = self.tableRobots[data.tableid][math.random(1, #self.tableRobots[data.tableid])]
			print("-------------->playerid", playerid, self.tbService[playerid])
			if self.tbService[playerid] then
				skynet.send(self.tbService[playerid], "lua", "mgr_sitdown_req")
			end			
			skynet.sleep(math.random(50, 500))
		end
	else--站起
		tbRobotSeat = {}
		for k,v in pairs(data.seat_settle) do
			if self:isRobet(data.tableid, v.playerid) then
				table.insert(tbRobotSeat, v)
			end
		end
		print("===============", seatNum - seatLimitNum)
		if #tbRobotSeat > 0 then
			for i=1, seatNum - seatLimitNum do
				local seat = table.remove(tbRobotSeat, math.random(1, #tbRobotSeat))
				print( "==============>seat", seat)
				local playerid = seat.playerid
				if self.tbService[playerid] then
					skynet.send(self.tbService[playerid], "lua", "mgr_standup_req")
				end			
				skynet.sleep(math.random(50, 500))
			end
		end
	end	
end

function zfb_robotMgr:isRobet(tableid, playerid)
	local tbPlayerid = self.tableRobots[tableid]
	if not table.empty(tbPlayerid) then
		for k, v in pairs(tbPlayerid) do
			if v == playerid then
				return true
			end
		end
	end
	return false
end

--请求添加机器人
function zfb_robotMgr:reqAddRoot(num)
	if not num or num == 0 then
		return
	end
	local tbPlayer = skynet.call("manager", "lua", "rb_get_account", num)
	print("------------1-tbPlayer", tbPlayer)
	if table.empty(tbPlayer) then
		return
	end	

	local tbRobot = {}
	for i, v in pairs(tbPlayer) do
		local uid = v.uid
		local sql = string.format("select * from usergame where uid = %d", uid)
		local res = skynet.call("mysql_service", "lua", "execute", sql)
		if table.empty(res) then
			break
		end
		local usergame = res [1]
		if usergame.uchip < 100000 then
			local data =  {money = math.random(200000, 9999999), uid = uid, game = 0, gameid = self.gameid, code = 0, gametype = 91}
			skynet.call("manager", "lua", "change_money", data)
		end
		self:addRobot({mid = v.uid})
	end
end

--检查机器人数量,少了就补充
function zfb_robotMgr:checkAddRoot()
	skynet.fork( function()
		while true do	
			print("--------------------------->zfb_robotMgr checkAddRoot")				
			for k, v in pairs(self.tableRobots) do
				log.debug(string.format("------------------------->checkAddRoot tableid:%d, robot Count:%d", k, table.size(v)))
				if table.size(v) < self.createNum then					
					self:reqAddRoot(self.createNum - table.size(v))
				end			
			end
			skynet.sleep(12000)
		end
	end)
end


return zfb_robotMgr